package com.beidouin.GnssMain.adapter;

import android.content.Context;
import android.location.LocationManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.ImageView;
import com.beidouin.GnssMain.R;
import com.beidouin.GnssMain.models.BeidouSatellite;
import com.beidouin.GnssMain.models.BeidouStatus;
import com.beidouin.GnssMain.models.PilotInfo;

import java.util.HashMap;
import java.util.Iterator;

/**
 * Created by yuyang on 16/6/24.
 */
public class SatelliteAdapter extends UAppendableListAdapter {
    private Context mContext;
    private final int VERSION_TYPE_COUNT = 1;

    private LocationManager locationManager;

    public static HashMap<String, Integer> getBright() {
        HashMap<String, Integer> bright = new HashMap<>();
        int bright1 = 1;
        int bright2 = 1;
        int bright3 = 1;
        int bright4 = 1;
        int bright5 = 1;
        BeidouStatus.getInstance().setData(PilotInfo.getBeidouSatelliteHashMap());
        for (BeidouSatellite version:BeidouStatus.getInstance().getSatellitesList()){
            if (version.getSateType() == BeidouSatellite.SATE_GPS) {
                if(version.getIsUsed() == true) {
                    bright.put("SATE_GPS",bright1++);
                }
            } else if (version.getSateType() == BeidouSatellite.SATE_GLONASS) {
                if(version.getIsUsed() == true) {
                    bright.put("SATE_GLONASS",bright2++);
                }
            } else if (version.getSateType() == BeidouSatellite.SATE_GALILEO) {
                if(version.getIsUsed() == true) {
                    bright.put("SATE_GALILEO",bright3++);
                }
            } else if (version.getSateType() == BeidouSatellite.SATE_BEIDOU) {
                if(version.getIsUsed() == true) {
                    bright.put("SATE_BEIDOU",bright4++);

                }
            } else if (version.getSateType() == BeidouSatellite.SATE_QZSS) {
                if(version.getIsUsed() == true) {
                    bright.put("SATE_QZSS",bright5++);
                }
            }
        }
        return bright;
    }



    public static HashMap<String, Integer> getDestroy() {
        HashMap<String, Integer> destroy = new HashMap<>();
        int destroy1 = 0;
        int destroy2 = 0;
        int destroy3 = 0;
        int destroy4 = 0;
        int destroy5 = 0;
        BeidouStatus.getInstance().setData(PilotInfo.getBeidouSatelliteHashMap());
        for (BeidouSatellite version:BeidouStatus.getInstance().getSatellitesList()){
            if (version.getSateType() == BeidouSatellite.SATE_GPS) {
                if(version.getIsUsed() == true){

                } else {
                    destroy.put("SATE_GPS",destroy1++);

                }
            } else if (version.getSateType() == BeidouSatellite.SATE_GLONASS) {
                if(version.getIsUsed() == true) {

                } else {

                    destroy.put("SATE_GLONASS",destroy2++);

                }
            } else if (version.getSateType() == BeidouSatellite.SATE_GALILEO) {
                if(version.getIsUsed() == true) {

                } else {
                    destroy.put("SATE_GALILEO",destroy3++);

                }
            } else if (version.getSateType() == BeidouSatellite.SATE_BEIDOU) {
                if(version.getIsUsed() == true) {

                } else {
                    destroy.put("SATE_BEIDOU",destroy4++);

                }
            } else if (version.getSateType() == BeidouSatellite.SATE_QZSS) {
                if(version.getIsUsed() == true) {

                } else {

                    destroy.put("SATE_QZSS",destroy5++);

                }
            }
        }



        return destroy;
    }

    public SatelliteAdapter(Context context) {
        super(context);
        this.mContext = context;

    }
    @Override
    public int getImplViewTypeCount() {
        return VERSION_TYPE_COUNT + 1;
    }

    @Override
    public int getImplItemViewType(int position) {
        if (position == 0 || position == 1) {
            return position;
        }
        return VERSION_TYPE_COUNT;
    }

    public int getImplCount() {
        return super.getImplCount() + VERSION_TYPE_COUNT;
    }

    @Override
    protected View getImplView(int position, View convertView, ViewGroup parent) {
        if (getItemViewType(position) == 0) {
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.satellite_detail_header, null);
            }
            return convertView;
        } else {
            BeidouSatellite item = (BeidouSatellite) getItem(position - VERSION_TYPE_COUNT);
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.satellite_detail, null);
            }
            if(item != null && item.getmSnr() != 0.0f) initVersionItemView(convertView,item);
            return convertView;
        }
    }



    private void initVersionItemView(View rootView, final BeidouSatellite version) {
        VersionView versionView;
        if (version.getmSnr() <= 0)
            return;
        if (rootView.getTag() == null) {
            versionView = new VersionView().init(rootView);
            rootView.setTag(versionView);
        } else {
            versionView = (VersionView) rootView.getTag();
        }
        versionView.idView.setText("  "+String.valueOf(version.getMsvId()));
        versionView.azimuthView.setText(String.valueOf((int)version.getmAzimuth()));
        versionView.snrView.setText(String.valueOf((int)version.getmSnr()));

        versionView.signalView.setText(String.valueOf((int)version.getmElevation()));
        ViewGroup.LayoutParams params =versionView.powerView.getLayoutParams();
        params.width = Math.round(550 * (version.getmSnr() / 50f));
        versionView.powerView.setLayoutParams(params);

        if(version.getIsUsed()){

            versionView.powerView.setBackgroundColor(mContext.getResources().getColor(R.color.app_main));
        }else{
            versionView.powerView.setBackgroundColor(mContext.getResources().getColor(R.color.light_black_overlay_pressed));
            //light_black_overlay_pressed 灰颜色
        }

        if (version.getSateType() == BeidouSatellite.SATE_GPS) {
            if(version.getIsUsed() == true) {
                versionView.sateView.setImageResource(R.drawable.gps);
                Log.i("snr","========"+versionView.snrView+BeidouSatellite.SATE_GPS);
            } else {
                versionView.sateView.setImageResource(R.drawable.gps2);

            }
        } else if (version.getSateType() == BeidouSatellite.SATE_GLONASS) {
            if(version.getIsUsed() == true) {
                versionView.sateView.setImageResource(R.drawable.glonass);

            } else {
                versionView.sateView.setImageResource(R.drawable.glonass2);

            }
        } else if (version.getSateType() == BeidouSatellite.SATE_GALILEO) {
            if(version.getIsUsed() == true) {
                versionView.sateView.setImageResource(R.drawable.galileo);

            } else {
                versionView.sateView.setImageResource(R.drawable.galileo2);

            }
        } else if (version.getSateType() == BeidouSatellite.SATE_BEIDOU) {
            if(version.getIsUsed() == true) {
                versionView.sateView.setImageResource(R.drawable.beidou);

            } else {
                versionView.sateView.setImageResource(R.drawable.beidou2);

            }
        } else if (version.getSateType() == BeidouSatellite.SATE_QZSS) {
            if(version.getIsUsed() == true) {
                versionView.sateView.setImageResource(R.drawable.qzss);

            } else {
                versionView.sateView.setImageResource(R.drawable.qzss);
                versionView.powerView.setBackgroundColor(mContext.getResources().getColor(R.color.app_main));
            }
        }

    }


    public void clearEmpty() {
        try {
            Iterator<Object> sListIterator = mData.iterator();
            while (sListIterator.hasNext()) {
                BeidouSatellite satellite = (BeidouSatellite) sListIterator.next();
                if (satellite == null) {
                    sListIterator.remove();
                    continue;
                }
                if (satellite.getmSnr() == 0.0f) {
                    sListIterator.remove();
                }
            }
        } catch (Exception e)
        {
            System.out.println("Exception in SatelliteAdapter:clearEmpty");
        }
    }



    private class VersionView {
        TextView idView;
        TextView snrView;
        TextView azimuthView;
        TextView signalView;
        View powerView;
        ImageView sateView;

        VersionView init(View rootView) {
            idView = (TextView) rootView.findViewById(R.id.satelliteId);
            snrView = (TextView) rootView.findViewById(R.id.satelliteSnr);
            azimuthView = (TextView) rootView.findViewById(R.id.satelliteAzimuth);
            signalView = (TextView) rootView.findViewById(R.id.satelliteSignal);
            powerView = (View) rootView.findViewById(R.id.SignalView);
            sateView = (ImageView) rootView.findViewById(R.id.satelliteIcon);
            return this;
        }
    }
}
